#include<iostream>
#include<queue>
using namespace std;
typedef struct TreeNode
{
	int val;
	struct TreeNode* left;
	struct TreeNode* right;
}TreeNode;
TreeNode* createT(int pre[], int mid[],int begin,int end,int* root,int n)
{
	if (begin > end || begin >= n || end >= n||*root>=n)
	{
		return nullptr;
	}
	TreeNode* newn = new TreeNode;
	newn->val = pre[*root];
	newn->left = nullptr;
	newn->right = nullptr;
	int i = 0;
	for (i=begin; i <= end; i++)
	{
		if (mid[i] == pre[*root])
			break;
	}
	int midi = i;
	(*root)++;
	newn->left = createT(pre, mid, begin, midi - 1, root , n);
	if(newn->left!=nullptr&&newn->right!=nullptr)
	(*root)++;
	newn->right= createT(pre, mid, midi + 1, end, root , n);
	return newn;
		
}
queue<int> q;

void inorder(TreeNode* root)
{
	if (root == nullptr)
		return;
	inorder(root->left);
	q.push(root->val);
	inorder(root->right);
}
int is_BT(TreeNode* root)
{
	inorder( root);
	int k = q.front();
	q.pop();
	while (!q.empty())
	{
		if (q.front() < k)
			return 0;
		k = q.front();
		q.pop();
	}
	return 1;
}
int main()
{
	int pre[200] = { 0 };
	int mid[200] = { 0 };
	int i = 0;
	for (i;; i++)
	{
		cin >> pre[i];
		//cout << pre[i] << ' ';
		if (pre[i] == 0)
			break;
	}
	//cout << endl;
	int n = i;
	for (i=0;; i++)
	{
		cin >> mid[i];
		//cout << mid[i] << ' ';
		if (mid[i] == 0)
			break;
	}
	int k = 0;
	TreeNode* t = createT(pre, mid, 0, n - 1, &k, n);
	int flag = is_BT(t);
	if (flag)
		cout << "true" << endl;
	else
		cout << "false" << endl;
}